package com.haixiaoke.saas.enterprise.contract.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.contract.domain.Proprietor;
import com.haixiaoke.saas.contract.service.IProprietorService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 业主信息Controller
 *
 * @author xiaolong
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/enterprise/contract/proprietor")
public class EnterpriseProprietorController extends BaseController {
    @Autowired
    private IProprietorService proprietorService;

    /**
     * 查询业主信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:proprietor:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(Proprietor proprietor) {
        startPage();
        List<Proprietor> list = proprietorService.selectProprietorList(proprietor);
        return getDataTable(list);
    }

    /**
     * 导出业主信息列表
     */
    @PostMapping("/export")
    @TenantScope
    @RepeatSubmit
    public void export(HttpServletResponse response, Proprietor proprietor) {
        proprietor.setTenantId(TenantContext.getTenantId());
        List<Proprietor> list = proprietorService.selectProprietorList(proprietor);
        ExcelUtil<Proprietor> util = new ExcelUtil<Proprietor>(Proprietor.class);
        util.exportExcel(response, list, "业主信息数据");
    }

    /**
     * 获取业主信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:proprietor:query')")
    @GetMapping(value = "/{proprietorId}")
    public AjaxResult getInfo(@PathVariable("proprietorId") Long proprietorId) {
        return success(proprietorService.selectProprietorByProprietorId(proprietorId));
    }

    /**
     * 新增业主信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:proprietor:add')")
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Valid Proprietor proprietor) {
        proprietor.setTenantId(TenantContext.getTenantId());
        proprietor.setCreateBy(getLoginUser().getUserId().toString());
        proprietor.setCreateOrganization(getLoginUser().getUsername());
        return toAjax(proprietorService.insertProprietor(proprietor));
    }

    /**
     * 修改业主信息
     */
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Proprietor proprietor) {
        return toAjax(proprietorService.updateProprietor(proprietor));
    }

    /**
     * 删除业主信息
     */
    @DeleteMapping("/{proprietorIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] proprietorIds) {
        return toAjax(proprietorService.deleteProprietorByProprietorIds(proprietorIds));
    }
}
